﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Xpo;
using WROA.Models.Entitys;
using DevExpress.Data.Filtering;
using WROA.Models.IServices;

namespace WROA.Models.Services
{
    public class MessageService : Services,IMessageService
    {
        /// <summary>
        /// 返回用户收信箱
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public XPCollection GetReceiveMessage(string name)
        {
            XPCollection xp = new XPCollection(typeof(Message), new BinaryOperator("Msg_receiver", name, BinaryOperatorType.Equal));
            SortProperty sp = new SortProperty("Msg_sendtime", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 返回用户发信箱
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public XPCollection GetSendMessage(string name)
        {
            XPCollection xp = new XPCollection(typeof(Message), new BinaryOperator("Msg_sender", name, BinaryOperatorType.Equal));
            SortProperty sp = new SortProperty("Msg_sendtime", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }



        /// <summary>
        /// 更新短信为已读
        /// </summary>
        /// <param name="id"></param>
        public void UpdateIsRead(int id)
        {
            XPCollection xp = new XPCollection(typeof(Message), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (Message m in xp)
            {
                m.Msg_isread = "是";
                m.Save();
            }
        }

        /// <summary>
        /// 根据id返回一个短信对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Message GetMessage(int id)
        {
            Message msg = null;
            XPCollection xp = new XPCollection(typeof(Message), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (Message m in xp)
            {
                msg = m;
            }
            return msg;
        }

        /// <summary>
        /// 单发短信
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="receiverList"></param>
        /// <param name="content"></param>
        public void SendMessage(string sender, string receiverList, string content)
        {
            Message message = new Message();
            message.Msg_sender = sender;
            message.Msg_receiver = receiverList;
            message.Msg_content = content;
            message.Msg_sendtime = DateTime.Now;
            message.Msg_isread = "否";
            message.Save();
        }

        /// <summary>
        /// 群发短信，用于流程批准后的通知
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="receiverList"></param>
        /// <param name="content"></param>
        public void MassTexting (string sender, List<string> receiverList, string content)
        {
            for (int i = 0; i < receiverList.Count; i++)
            {
                Message message = new Message();
                message.Msg_sender = sender;
                message.Msg_receiver = receiverList[i];
                message.Msg_content = content;
                message.Msg_sendtime = DateTime.Now;
                message.Msg_isread = "否";
                message.Save();
            }
        }
    }
}